<?php

namespace app\admin\controller;

use think\Controller;
use Util\SysDb;
use think\Db;

class Goodinfo extends Controller
{
    public function getstore()
    {
        $this->db = new SysDb();
        $admin = $this->db->table('goods_store')->where(['status' => 1])->lists();
        if (!$admin) {
            exit(json_encode(['code' => 1, 'message' => 'error', "result" => "无仓库"]));
        } else {
            exit(json_encode(['message' => 'succ', 'result' => $admin]));
        }
    }

    public function goodstypes()
    {
        $this->db = new SysDb();
        $admin = $this->db->table('goods_type')->lists();
        if (!$admin) {
            exit(json_encode(['code' => 1, 'message' => 'error', "result" => "无仓库"]));
        } else {
            exit(json_encode(['message' => 'succ', 'result' => $admin]));
        }
    }
    public function setstatus()
    {
        $id = trim(input('param.goodsId'));
        $status = trim(input('param.status'));
        $this->db = new SysDb();
        $admin = $this->db->table('goods_info')->where(['id ' => $id])->item();
        if (!$admin) {
            exit(json_encode(['code' => 1, 'message' => 'error', "result" => "无仓库"]));
        } else {
            $admin['status'] = $status;
            $this->db->table('goods_info')->where(['id' => $id])->update($admin);
            exit(json_encode(['message' => 'succ', 'result' => "修改成功"]));
        }
    }
    public function goodsdel()
    {
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $admin = Db::name('goods_info')->where('id', $id)->delete();;
        if ($admin) {
            exit(json_encode(['message' => 'succ', 'result' => "修改成功"]));
        }
    }
    public function add()
    {
        $name = trim(input('param.name'));
        $storeid = trim(input('param.storeId'));
        $descr = trim(input('param.descr'));
        $cost = trim(input('param.cost'));
        $tenantCost = trim(input('param.tenantCost'));
        $weight = trim(input('param.weight'));
        $repertory = trim(input('param.repertory'));
        $status = trim(input('param.status'));
        $salesNum = trim(input('param.salesNum'));
        $otherType = trim(input('param.other_type'));
        $types = trim(input('param.types'));
        $img = trim(input('param.cover'));
        $imageName = "25220_" . date("His", time()) . "_" . rand(1111, 9999) . '.png';
        if (strstr($img, ",")) {
            $image = explode(',', $img);
            $image = $image[1];
        }
        $path = "./uploads/" . date("Ymd", time());
        $paths = "http://127.0.0.1:8000/uploads/" . date("Ymd", time());
        if (!is_dir($path)) { //判断目录是否存在 不存在就创建
            mkdir($path, 0777, true);
        }
        $imageSrc = $path . "/" . $imageName; //图片名字
        $r = file_put_contents($imageSrc, base64_decode($image)); //返回的是字节数
        $imageSrcs = $paths . "/" . $imageName; //图片名字
        if (!$r) {
            exit(json_encode(['message' => 'error', 'result' => "失败"]));
        } else {
            $this->db = new SysDb();
            $admin['name'] = $name;
            $admin['cover'] = $imageSrcs;
            $admin['store_id'] = $storeid;
            $admin['descr'] = $descr;
            $admin['cost'] = $cost;
            $admin['tenant_cost'] = $tenantCost;
            $admin['weight'] = $weight;
            $admin['repertory'] = $repertory;
            $admin['status'] = $status;
            $admin['sales_num'] = $salesNum;
            $admin['other_type'] = 1;
            $admin['types'] =$types;
            $admins=Db::name('goods_info')->insert($admin);
            $userId = Db::name('user')->getLastInsID();
            if ($admins) {
                $tenant["profit"]=$tenantCost-$cost;
                $tenant["goods_id"]=$userId;
                $tenant["tenant_id"]=trim(input('param.tenantid'));
                $tenants=Db::name('tenant_goods_price')->insert($tenant);
                if($tenants){
                    exit(json_encode(['message' => 'succ', 'result' => "添加成功"]));
                }
               
            }
        }
    }
    public function update()
    {
        $name = trim(input('param.name'));
        $storeid = trim(input('param.storeId'));
        $types = trim(input('param.types'));
        $descr = trim(input('param.descr'));
        $cost = trim(input('param.cost'));
        $tenantCost = trim(input('param.tenantCost'));
        $weight = trim(input('param.weight'));
        $repertory = trim(input('param.repertory'));
        $status = trim(input('param.status'));
        $salesNum = trim(input('param.salesNum'));
        $otherType = trim(input('param.other_type'));
        $id = trim(input('param.id'));
        $img = trim(input('param.cover'));

        $needle = 'base64'; //判断是否包含a这个字符
        $tmparray = explode($needle, $img);
        if (count($tmparray) > 1) {
            $imageName = "25220_" . date("His", time()) . "_" . rand(1111, 9999) . '.png';
            if (strstr($img, ",")) {
                $image = explode(',', $img);
                $image = $image[1];
            }
            $path = "./uploads/" . date("Ymd", time());
            $paths = "http://127.0.0.1:8000/uploads/" . date("Ymd", time());
            if (!is_dir($path)) { //判断目录是否存在 不存在就创建
                mkdir($path, 0777, true);
            }
            $imageSrc = $path . "/" . $imageName; //图片名字
            $r = file_put_contents($imageSrc, base64_decode($image)); //返回的是字节数
            $imageSrcs = $paths . "/" . $imageName; //图片名字
            if (!$r) {
                exit(json_encode(['message' => 'error', 'result' => "失败"]));
            } else {
                $this->db = new SysDb();
                $admin = $this->db->table('goods_info')->where(['id ' => $id])->item();
                $admin['name'] = $name;
                $admin['cover'] = $imageSrcs;
                $admin['store_id'] = $storeid;
                $admin['descr'] = $descr;
                $admin['cost'] = $cost;
                $admin['tenant_cost'] = $tenantCost;
                $admin['weight'] = $weight;
                $admin['repertory'] = $repertory;
                $admin['status'] = $status;
                $admin['sales_num'] = $salesNum;
                $admin['types'] = $types;
                $admins = $this->db->table('goods_info')->where(['id' => $id])->update($admin);
                $tenant=$admin = $this->db->table('tenant_goods_price')->where(['goods_id ' => $id])->item();
                $tenant["profit"]=$tenantCost-$cost;
                $tenant["tenant_id"]=trim(input('param.tenantid'));
                $tenants=Db::name('tenant_goods_price')->update($tenant);
                if ($admins||$tenants) {
                    exit(json_encode(['message' => 'succ', 'result' => "修改成功"]));
                }
            }
        } else {
            $this->db = new SysDb();
                $admin = $this->db->table('goods_info')->where(['id ' => $id])->item();
                $admin['name'] = $name;
                $admin['cover'] = $img;
                $admin['store_id'] = $storeid;
                $admin['descr'] = $descr;
                $admin['cost'] = $cost;
                $admin['tenant_cost'] = $tenantCost;
                $admin['weight'] = $weight;
                $admin['repertory'] = $repertory;
                $admin['status'] = $status;
                $admin['sales_num'] = $salesNum;
                $admin['types'] = $types;
                $admins = $this->db->table('goods_info')->where(['id' => $id])->update($admin);
                $tenant=$admin = $this->db->table('tenant_goods_price')->where(['goods_id ' => $id])->item();
                $tenant["profit"]=$tenantCost-$cost;
                $tenant["tenant_id"]=trim(input('param.tenantid'));
                $tenants=Db::name('tenant_goods_price')->update($tenant);
                if ($admins||$tenants) {
                    exit(json_encode(['message' => 'succ', 'result' => "修改成功"]));
                }
        }
    }
    //     public function list()
    //     {
    //         // ->where(['store_id' => $storeId])->lists()
    //         $storeId = trim(input('param.id'));
    //         // $this->db = new SysDb();
    //         // $admin = $this->db->table('goods_info')->join('as gi left join goods_store as gs on gi.store_id = gs.id left join tenant_goods_price as tgp on gi.id = tgp.gooods_id')->where(['store_id' => $storeId])->lists();
    //         $admin= Db::table('goods_info')
    //             ->alias(' gi')
    //             ->join('goods_store gs', 'gi.store_id = gs.id',"left")
    //             ->join('tenant_goods_price tgp', 'gi.id = tgp.goods_id',"left")
    //             ->where(['store_id' => $storeId])
    //             ->field(array(' gi.store_id' =>'storeId','gi.types'=>'types','gi.id'=>"giId",'gi.status'=>"giStatus",'gi.cover','gi.name','gi.sales_num'=>'salesNum','gi.descr','gi.weight','gi.cost','tgp.id'=>'id','tgp.profit'=>'profit','gi.repertory'=>"repertory"))
    //             ->select();

    //         if (!$admin) {
    //             exit(json_encode(['code' => 1, 'message' => 'error', "result" => "无库"]));
    //         } else {
    //             exit(json_encode(['message' => 'succ', 'result' => $admin]));
    //         }
    //     }
    //     public function goodlist(){
    //          // ->where(['store_id' => $storeId])->lists()
    //          $storeId = trim(input('param.sort'));
    //          $page = trim(input('param.pageNo'));
    //          $limit= trim(input('param.pageSize'));
    //          $tol = (intval($page) - 1) * $limit;
    //          // $this->db = new SysDb();
    //          // $admin = $this->db->table('goods_info')->join('as gi left join goods_store as gs on gi.store_id = gs.id left join tenant_goods_price as tgp on gi.id = tgp.gooods_id')->where(['store_id' => $storeId])->lists();
    //          $admin= Db::table('goods_info')
    //              ->alias(' gi')
    //              ->join('goods_store gs', 'gi.store_id = gs.id',"left")
    //              ->join('tenant_goods_price tgp', 'gi.id = tgp.goods_id',"left")
    //              ->where(['sort' => $storeId])
    //              ->limit($tol, $limit)
    //              ->field(array(' gi.store_id' =>'storeId','gi.types'=>'types','gi.id'=>"giId",'gi.status'=>"giStatus",'gi.cover','gi.name','gi.sales_num'=>'salesNum','gi.descr','gi.weight','gi.cost','tgp.id'=>'id','tgp.profit'=>'profit','gi.repertory'=>"repertory"))
    //              ->select();

    //          if (!$admin) {
    //              exit(json_encode(['code' => 1, 'message' => 'error', "result" => "无商品"]));
    //          } else {
    //             $admins= Db::table('goods_info')
    //             ->alias(' gi')
    //             ->join('goods_store gs', 'gi.store_id = gs.id',"left")
    //             ->join('tenant_goods_price tgp', 'gi.id = tgp.goods_id',"left")
    //             ->where(['sort' => $storeId])
    //             ->field(array(' gi.store_id' =>'storeId','gi.types'=>'types','gi.id'=>"giId",'gi.status'=>"giStatus",'gi.cover','gi.name','gi.sales_num'=>'salesNum','gi.descr','gi.weight','gi.cost','tgp.id'=>'id','tgp.profit'=>'profit','gi.repertory'=>"repertory"))
    //             ->select();
    //              $len=count($admins);
    //              $list=array();
    //              array_push($list,$admin);
    //              array_push($list,$len);
    //              exit(json_encode(['message' => 'succ', 'result' => $list]));
    //          }
    //     }
    public function goodlists()
    {
        // ->where(['store_id' => $storeId])->lists()
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        $name  = trim(input('param.name'));
        $storeId  = trim(input('param.storeId'));
        $types  = trim(input('param.types'));
        $where=array(
            "types" => $types,
            "store_id" => $storeId,
            "name" => $name
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $list = DB::name("goods_info")->order("create_time desc")->where($where)->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('goods_info')->where($where)->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }
    //     public function newdetail()
    //     {
    //         // ->where(['store_id' => $storeId])->lists()
    //         $storeId = trim(input('param.goodsId'));
    //         // $this->db = new SysDb();
    //         // $admin = $this->db->table('goods_info')->join('as gi left join goods_store as gs on gi.store_id = gs.id left join tenant_goods_price as tgp on gi.id = tgp.gooods_id')->where(['store_id' => $storeId])->lists();
    //         $admin= Db::table('goods_info')
    //             ->alias(' gi')
    //             ->join('goods_store gs', 'gi.store_id = gs.id',"left")
    //             ->join('tenant_goods_price tgp', 'gi.id = tgp.goods_id',"left")
    //             ->where(['goods_id' => $storeId])
    //             ->field(array(' gi.store_id' =>'storeId','gi.types'=>'types','gi.id'=>"giId",'gi.status'=>"giStatus",'gi.cover','gi.name','gi.sales_num'=>'salesNum','gi.descr','gi.weight','gi.cost','tgp.id'=>'id','tgp.profit'=>'profit','gi.repertory'=>"repertory"))
    //             ->select();

    //         if (!$admin) {
    //             $admins= Db::table('goods_info')
    //             ->alias(' gi')
    //             ->join('goods_store gs', 'gi.store_id = gs.id',"left")
    //             ->join('tenant_goods_price tgp', 'gi.id = tgp.goods_id',"left")
    //             ->where(['goods_id' => 105])
    //             ->field(array(' gi.store_id' =>'storeId','gi.types'=>'types','gi.id'=>"giId",'gi.status'=>"giStatus",'gi.cover','gi.name','gi.sales_num'=>'salesNum','gi.descr','gi.weight','gi.cost','tgp.id'=>'id','tgp.profit'=>'profit','gi.repertory'=>"repertory"))
    //             ->select();

    //             exit(json_encode(['message' => 'succ', 'result' => $admins[0]]));
    //         } else {
    //             exit(json_encode(['message' => 'succ', 'result' => $admin]));
    //         }
    //     }
    //     public function detail()
    //     {
    //        $id = trim(input('param.id'));
    //        $this->db = new SysDb();
    //        $good = $this->db->table('goods_info')->where(['id' => $id])->item();
    //        $goodstroe=$this->db->table('goods_store')->where(['id' =>  $good['store_id']])->item();
    //        $data=array();
    //        array_push($data,$good);
    //        array_push($data,$goodstroe);
    //        exit(json_encode(['message' => 'succ', 'result' =>$data]));
    //     }
    //     public function getInviteCode(){
    //         $id = trim(input('param.id'));
    //         $encode = base64_encode($id );
    //         exit(json_encode(['message' => 'succ', 'result' =>$encode]));
    //     }
    //     public function levelList(){
    //         $tenantId = trim(input('param.tenantId'));
    //         $this->db = new SysDb();
    //         $list = $this->db->table('tenant_vip_level')->where(['tenant_id' => $tenantId])->where(['condition_num' => 0])->lists();
    //         $lista = $this->db->table('tenant_vip_level')->where(['tenant_id' => $tenantId])->where(['condition_num' => 3])->lists();
    //         $lists=array();
    //         array_push($lists,$list[0]);
    //         array_push($lists,$lista[0]);
    //          exit(json_encode(['message' => 'succ', 'result' => $lists]));
    //     }
    //     public function spreadList(){
    //         $id = trim(input('param.id'));
    //         $page = trim(input('param.pageNo'));
    //         $limit= trim(input('param.pageSize'));
    //         $tol = (intval($page) - 1) * $limit;
    //         $list =DB::name("bus_info")->order("create_time desc")->where('parent_id', $id)->limit($tol, $limit)->select();
    //         $this->db = new SysDb();
    //         $count = $this->db->table('bus_info')->where(['parent_id' => $id]) ->count();
    //         $data=array();
    //         array_push($data,$list);
    //         array_push($data,$count);
    //         exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    //         // $id = trim(input('param.id'));
    //         // $this->db = new SysDb();
    //         // $list = $this->db->table('bus_info')->where(['parent_id' => $id])->lists();
    //         // exit(json_encode(['message' => 'succ', 'result' => $list]));
    //     }

}
